#pragma once

#include <iostream>
#include <memory>
#include "Protocol.hpp"


// 业务
enum
{
    Success = 0,
    DivZeroErr,     // 除0错误
    ModZeroErr,     // 模0错误
    UnKnowOper      // 未知运算符
};

class Calculate
{
public:
    Calculate() {}

    std::shared_ptr<Response> Cal(std::shared_ptr<Request> req)
    {
        std::shared_ptr<Response> resp = _factory.BuildResponse();
        resp->SetCode(Success);

        switch (req->GetOper())
        {
        case '+':
            resp->SetResult(req->GetX() + req->GetY());
            break;
        case '-':
            resp->SetResult(req->GetX() - req->GetY());
            break;
        case '*':
            resp->SetResult(req->GetX() * req->GetY());
            break;
        case '/':
        {
            if (req->GetY() == 0)
            {
                resp->SetCode(DivZeroErr);
            }
            else
            {
                resp->SetResult(req->GetX() / req->GetY());
            }
            break;
        }
        case '%':
        {
            if (req->GetY() == 0)
            {
                resp->SetCode(ModZeroErr);
            }
            else
            {
                resp->SetResult(req->GetX() / req->GetY());
            }
        }
        break;
        default:
            resp->SetCode(UnKnowOper);
            break;
        }
        return resp;
    }

    ~Calculate() {}

private:
    Factory _factory;
};